﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.IO;
using System.Drawing.Printing;
using System.Drawing;


namespace MvcWeb.Controllers
{
    public class PrintController : BaseController
    {

        CodeLib.Barcode b = new CodeLib.Barcode();

        //
        // GET: /Manager/
        public ActionResult Index()
        {
            List<Image> imgList = new List<Image>();

            string ids = Request.Form["ids"];
            if (!string.IsNullOrWhiteSpace(ids))
            {
                string[] arr = ids.Split(',');

                if (arr != null)
                {
                    foreach (string id in arr)
                    {
                        EAM.Model.AssetManager assetModel = db.AssetManager.Find(int.Parse(id));

                        Image img = null;

                        MvcWeb.ViewModels.Manager.PrintSetting printSettingModel = new ViewModels.Manager.PrintSetting();
                        int W = Convert.ToInt32(printSettingModel._Width);
                        int H = Convert.ToInt32(printSettingModel._Height);
                        CodeLib.AlignmentPositions Align = CodeLib.AlignmentPositions.CENTER;

                        //barcode alignment
                        switch (printSettingModel.Align)
                        {
                            case "left": Align = CodeLib.AlignmentPositions.LEFT; break;
                            case "right": Align = CodeLib.AlignmentPositions.RIGHT; break;
                            default: Align = CodeLib.AlignmentPositions.CENTER; break;
                        }//switch

                        CodeLib.TYPE type = CodeLib.TYPE.UNSPECIFIED;
                        switch (printSettingModel.encodeType.ToString())
                        {
                            case "UPC-A": type = CodeLib.TYPE.UPCA; break;
                            case "UPC-E": type = CodeLib.TYPE.UPCE; break;
                            case "UPC 2 Digit Ext.": type = CodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;
                            case "UPC 5 Digit Ext.": type = CodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;
                            case "EAN-13": type = CodeLib.TYPE.EAN13; break;
                            case "JAN-13": type = CodeLib.TYPE.JAN13; break;
                            case "EAN-8": type = CodeLib.TYPE.EAN8; break;
                            case "ITF-14": type = CodeLib.TYPE.ITF14; break;
                            case "Codabar": type = CodeLib.TYPE.Codabar; break;
                            case "PostNet": type = CodeLib.TYPE.PostNet; break;
                            case "Bookland/ISBN": type = CodeLib.TYPE.BOOKLAND; break;
                            case "Code 11": type = CodeLib.TYPE.CODE11; break;
                            case "Code 39": type = CodeLib.TYPE.CODE39; break;
                            case "Code 39 Extended": type = CodeLib.TYPE.CODE39Extended; break;
                            case "Code 93": type = CodeLib.TYPE.CODE93; break;
                            case "LOGMARS": type = CodeLib.TYPE.LOGMARS; break;
                            case "MSI": type = CodeLib.TYPE.MSI_Mod10; break;
                            case "Interleaved 2 of 5": type = CodeLib.TYPE.Interleaved2of5; break;
                            case "Standard 2 of 5": type = CodeLib.TYPE.Standard2of5; break;
                            case "Code 128": type = CodeLib.TYPE.CODE128; break;
                            case "Code 128-A": type = CodeLib.TYPE.CODE128A; break;
                            case "Code 128-B": type = CodeLib.TYPE.CODE128B; break;
                            case "Code 128-C": type = CodeLib.TYPE.CODE128C; break;
                            case "Telepen": type = CodeLib.TYPE.TELEPEN; break;
                            default: type = CodeLib.TYPE.UPCA; break; ; break;
                        }//switch

                        try
                        {
                            if (type != CodeLib.TYPE.UNSPECIFIED)
                            {
                                b.IncludeLabel = printSettingModel.includeLabel;

                                b.Alignment = Align;
                                b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), printSettingModel._RotateFlipType.ToString(), true);

                                //label alignment and position
                                switch (printSettingModel._LabelPosition.ToString().ToUpper())
                                {
                                    case "BOTTOMLEFT": b.LabelPosition = CodeLib.LabelPositions.BOTTOMLEFT; break;
                                    case "BOTTOMRIGHT": b.LabelPosition = CodeLib.LabelPositions.BOTTOMRIGHT; break;
                                    case "TOPCENTER": b.LabelPosition = CodeLib.LabelPositions.TOPCENTER; break;
                                    case "TOPLEFT": b.LabelPosition = CodeLib.LabelPositions.TOPLEFT; break;
                                    case "TOPRIGHT": b.LabelPosition = CodeLib.LabelPositions.TOPRIGHT; break;
                                    default: b.LabelPosition = CodeLib.LabelPositions.BOTTOMCENTER; break;
                                }//switch


                                b.usedPeople = printSettingModel.usedPeople;
                                b.weihuPeople = printSettingModel.weihuPeople;
                                b.showText1 = printSettingModel.showText1;
                                b.showText2 = printSettingModel.showText2;
                                b.showText3 = printSettingModel.showText3;
                                b.showText4 = printSettingModel.showText4;

                                //===== Encoding performed here =====
                                img = b.Encode(type, assetModel.AssetLabel, printSettingModel._ForeColor, printSettingModel._BackColor, W, H);
                                //===================================

                                //show the encoding time
                                //this.lblEncodingTime.Text = "(" + Math.Round(b.EncodingTime, 0, MidpointRounding.AwayFromZero).ToString() + "ms)";

                                //txtEncoded.Text = b.EncodedValue;

                                //tsslEncodedType.Text = "Encoding Type: " + b.EncodedType.ToString();
                            }//if

                            //img.Width = img.Image.Width;
                            //img.Height = img.Image.Height;

                            imgList.Add(img);
                            //reposition the barcode image to the middle
                            //img.Location = new Point((this.groupBox2.Location.X + this.groupBox2.Width / 2) - barcode.Width / 2, (this.groupBox2.Location.Y + this.groupBox2.Height / 2) - barcode.Height / 2);
                        }//try
                        catch (Exception ex)
                        {
                            //MessageBox.Show(ex.Message);
                        }//catch

                    }
                }
            }
            return View(imgList);
        }



        //string filename = "Untitled";

        ////實例化打印文檔
        //PrintDocument pdDocument = new PrintDocument();

        ////１、打印格式設置頁面
        //PageSetupDialog dlgPageSetup = new PageSetupDialog();

        //string[] lines;
        //int linesPrinted;

        ////
        //// GET: /Print/

        //public ActionResult Index()
        //{


        //    //訂閱事件

        //    //訂閱PinrtPage事件,用於繪製各個頁內容
        //    pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
        //    //訂閱BeginPrint事件,用於得到被打印的內容
        //    pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
        //    //訂閱EndPrint事件,用於釋放資源
        //    pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);

        //    //２、頁面設置的打印文檔設置為需要打印的文檔
        //    dlgPageSetup.Document = pdDocument;

        //    return View();
        //}

        ///// <summary>
        ///// ３、在頁面設置按鈕事件中呼叫頁面設置界面，這樣單擊“頁面設置”按鈕就會彈出“頁面設置”界面
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void OnFilePageSetup(object sender, EventArgs e)
        //{
        //    dlgPageSetup.ShowDialog();
        //}

        //private void OnFilePrint(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        //調用打印
        //        pdDocument.Print();

        //        /*
        //         * PrintDocument對象的Print()方法在PrintController類的幫助下，執行PrintPage事件。
        //         */
        //    }
        //    catch (InvalidPrinterException ex)
        //    {
        //        //MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        throw;
        //    }
        //}

        ///// <summary>
        ///// 得到打印內容
        ///// 每個打印任務衹調用OnBeginPrint()一次。
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void pdDocument_BeginPrint(object sender, PrintEventArgs e)
        //{
        //    char[] param = { '\n' };
        //    lines = textBoxEdit.Text.Split(param);

        //    int i = 0;
        //    char[] trimParam = { '\r' };
        //    foreach (string s in lines)
        //    {
        //        lines[i++] = s.TrimEnd(trimParam);
        //    }
        //}




        ///// <summary>
        ///// 繪制多個打印頁面
        ///// printDocument的PrintPage事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void OnPrintPage(object sender, PrintPageEventArgs e)
        //{
        //    /*
        //     * 得到TextBox中每行的字符串數組
        //     * \n換行
        //     * \r回車
        //     */

        //    int x = 20;
        //    int y = 20;
        //    while (linesPrinted < lines.Length)
        //    {
        //        //繪製要打印的頁面
        //        e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);

        //        y += 55;

        //        //判斷超過一頁時，允許進行多頁打印
        //        if (y >= e.PageBounds.Height - 80)
        //        {
        //            //允許多頁打印
        //            e.HasMorePages = true;

        //            /*
        //             * PrintPageEventArgs類的HaeMorePages屬性為True時，通知控件器，必須再次調用OnPrintPage()方法，打印一個頁面。
        //             * PrintLoopI()有一個用於每個要打印的頁面的序例。如果HasMorePages是False，PrintLoop()就會停止。
        //             */
        //            return;
        //        }
        //    }

        //    linesPrinted = 0;
        //    //繪制完成後，關閉多頁打印功能
        //    e.HasMorePages = false;

        //}

        ///// <summary>  
        /////EndPrint事件,釋放資源
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void pdDocument_EndPrint(object sender, PrintEventArgs e)
        //{
        //    //變量Lines占用和引用的字符串數組，現在釋放
        //    lines = null;
        //}


    }
}
